<?php
namespace app\back\controller;
use think\Controller;
use think\Db;
use think\Request;
/*管理员管理*/
class Admin extends Common{
    /**管理员列表**/
    public function admin_list(){
         $adminList=Db::name('admin')->paginate(5,false);
         $page = $adminList->render();
         $this->assign('adminList',$adminList);
         $this->assign('page',$page);
         return  $this->fetch('Admin/admin_list');

    }
    /*添加管理员*/
     public function admin_add(){
        //查询角色
        $roleList=Db::name('role')->select();
        $this->assign('roleList',$roleList);
         return  $this->fetch('Admin/admin_add');

    }
    //删除角色
    public function roledel(){
        $role_id=Request::instance()->post('role_id');
        $data=Think_role::destroy(['role_id'=>$role_id]);
        if($data){
            echo 1;
        }else{
            echo 0;
        }
    }
   
    //处理管理员
    public function userto(){
        $data=input('post.');
        $username=Request::instance()->post('username');
        $userPhone=Request::instance()->post('userPhone');
        $password=Request::instance()->post('password');
        $a_role=Request::instance()->post('a_role');
        if(empty($a_role)){
            return json(['state'=>'0','msg'=>'请选择角色']);
        }
        //经行密码加密
        $pwd=md5($password);
         //查询是否有重复的
        $re=Db::name('admin')->where(['username'=>$username])->find();
        if($re){
            return json(['state'=>'0','msg'=>'用户名已经存在']);
        }
        $rs=Db::name('admin')->where(['userPhone'=>$userPhone])->find();
        if($rs){
             return json(['state'=>'0','msg'=>'该手机号已经添加过啦']);
        }
        //查询会员列表的领队uid
       $rd=Db::name('user_info')->where(['userPhone'=>$userPhone,'member_type'=>'2'])->field('uid')->find();
       if(empty($rd)){
         return json(['state'=>'0','msg'=>'未匹配出该领队']);
       }else{
        $data['uid']=$rd['uid'];
       }
        $data['username']=$username;$data['userPhone']=$userPhone;
        $data['password']=$pwd;$data['create_time']=data("Y-m-d H:s:i");
        $result=Db::name('admin')->insert($data);
        if($result){
               return json(['state'=>'1','msg'=>'添加成功']);
            }else{
              return json(['state'=>'0','msg'=>'添加失败再试试']);
            }
        }

   
}